part of _;

extension on CartPage {
  void loadData(bool isAdd) {
    page = isAdd ? page + 1 : 1;
    yPost<CartListModel>(IFinal.Jkids.getCarts, {
      "page": page,
    }, (isSuccess, results, code, message) async {
      page = isSuccess ? (isAdd ? (results!.first.items_carts.isEmpty ? page - 1 : page) : page) : (isAdd ? page - 1 : page);
      if (isSuccess) {
        if (!isAdd)
          model_carts = results!.first;
        else
          model_carts!.items_carts.addAll(results!.first.items_carts);
        setState(() {});
      } else {
        if (!isAdd) {
          if (await AppUtils.checkIsLogon(yPage)) yShowBuilder("数据加载失败，是否重试？\n$message", () => loadData(isAdd), null, true);
        }
      }
      myRefresherController?.loadFinish(showNoMoreData: results?.firstOrNull?.items_carts.isEmpty ?? false);
    }, onModel: (map) => CartListModel.fromJson(map), showProgress: false, cacheEnable: !isAdd);
  }

  bool getIsAllSelect() {
    return model_carts?.items_carts.where((element) => element.countLeft > 0 && !element.isSelect).isEmpty ?? false;
  }

  List<CartMainModel> getSelectModels() {
    List<CartMainModel> results = [];
    model_carts?.items_carts.forEach((item_carts) {
      if (item_carts.isSelect) results.add(item_carts);
    });
    return results;
  }

  double getSelectTotalMoney() {
    double total = 0.0;
    getSelectModels().forEach((item) {
      total += item.count_input * double.parse(item.money);
    });
    return total;
  }
}
